﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Lavanderia_List : System.Web.UI.Page
{
    public List<BLLLaundry.Full_Detail> details
    {
        get { return (List<BLLLaundry.Full_Detail>)Session["detail"]; }
        set { Session["detail"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BLLProcess DB = new BLLProcess();
            List<Process> processes = DB.GetProcesses();
            List<BLLLaundry.Full_Detail> list = new List<BLLLaundry.Full_Detail>();
            for (int i = 1; i < 11; i++)
            {
                BLLLaundry.Full_Detail d = new BLLLaundry.Full_Detail();
                d.Id = i * -1;
                d.Processes = processes;
                list.Add(d);
            }
            details = list;
            bindGrid();
        }
    }
    private void bindGrid() {
        grid.DataSource = details;
        grid.DataBind();
    }
    protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                BLLLaundry.Full_Detail detail = (BLLLaundry.Full_Detail)e.Row.DataItem;
                DropDownList drd = (DropDownList)e.Row.FindControl("drdProcess");
                TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
                TextBox txtPrice = (TextBox)e.Row.FindControl("txtPrice");
                HiddenField hdnId = (HiddenField)e.Row.FindControl("hdnId");
                drd.Items.Clear();
                drd.Items.Add(new ListItem("", ""));
                foreach (Process p in detail.Processes)
                {
                    drd.Items.Add(new ListItem(p.Name, p.Id.ToString()));
                }
                drd.SelectedValue = detail.ProcessId.ToString();
                txtQuantity.Text = detail.Quantity;
                txtPrice.Text = detail.Price;
            }
        }
        catch { }
    }
   
    protected void grid_RowCreated(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Visible = false;
                //Row Sub Total
                GridViewRow rowFooter = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
                Literal newCells = new Literal();
                newCells.Text = "Sub-Total: &nbsp;";
                TableCell footerTaxCell1 = new TableCell();
                footerTaxCell1.Controls.Add(newCells);
                footerTaxCell1.ColumnSpan = 3;
                footerTaxCell1.CssClass = "totals";
                rowFooter.Cells.Add(footerTaxCell1);
                TextBox total = new TextBox();
                total.CssClass = "detail-input footer-subtotal";
                total.ReadOnly = true;
                TableCell footerTaxCell2 = new TableCell();
                footerTaxCell2.Controls.Add(total);
                rowFooter.Cells.Add(footerTaxCell2);
                rowFooter.Visible = true;
                grid.Controls[0].Controls.AddAt(grid.Controls[0].Controls.Count, rowFooter);
                //Row Tax
                GridViewRow rowFooter1 = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
                newCells = new Literal();
                newCells.Text = "IVA: &nbsp;";
                footerTaxCell1 = new TableCell();
                footerTaxCell1.Controls.Add(newCells);
                footerTaxCell1.ColumnSpan = 3;
                footerTaxCell1.CssClass = "totals";
                rowFooter1.Cells.Add(footerTaxCell1);
                total = new TextBox();
                total.CssClass = "detail-input footer-tax";
                total.ReadOnly = true;
                footerTaxCell2 = new TableCell();
                footerTaxCell2.Controls.Add(total);
                rowFooter1.Cells.Add(footerTaxCell2);
                rowFooter1.Visible = true;
                grid.Controls[0].Controls.AddAt(grid.Controls[0].Controls.Count, rowFooter1);
                //Row Total
                GridViewRow rowFooter2 = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
                newCells = new Literal();
                newCells.Text = "Total: &nbsp;";
                footerTaxCell1 = new TableCell();
                footerTaxCell1.Controls.Add(newCells);
                footerTaxCell1.ColumnSpan = 3;
                footerTaxCell1.CssClass = "totals";
                rowFooter2.Cells.Add(footerTaxCell1);
                total = new TextBox();
                total.CssClass = "detail-input footer-total";
                total.ReadOnly = true;
                footerTaxCell2 = new TableCell();
                footerTaxCell2.Controls.Add(total);
                rowFooter2.Cells.Add(footerTaxCell2);
                rowFooter2.Visible = true;
                grid.Controls[0].Controls.AddAt(grid.Controls[0].Controls.Count, rowFooter2);
            }
        }
        catch { }
    }
    protected void imgAdd_Click(object sender, ImageClickEventArgs e)
    {
        buildDetails();
        long min = (from m in details select m.Id).Min();
        if (min > 0)
            min = -1;
        else
            min -= 1;
        BLLProcess DB = new BLLProcess();
        List<Process> processes = DB.GetProcesses();
        BLLLaundry.Full_Detail d = new BLLLaundry.Full_Detail();
        d.Id = min;
        d.Processes = processes;
        details.Add(d);
        bindGrid();
    }
    private void buildDetails()
    {
        for (int i = 0; i < grid.Rows.Count; i++ )
        {
            try
            {
                DropDownList drdProcess = (DropDownList)grid.Rows[i].FindControl("drdProcess");
                TextBox txtQuantity = (TextBox)grid.Rows[i].FindControl("txtQuantity");
                TextBox txtPrice = (TextBox)grid.Rows[i].FindControl("txtPrice");
                HiddenField hdnId = (HiddenField)grid.Rows[i].FindControl("hdnId");
                int id = 0;
                int.TryParse(hdnId.Value, out id);
                if (drdProcess.SelectedValue != "" && txtQuantity.Text != "" && txtPrice.Text != "")
                {
                    if (id < 0)
                    {
                        id = id * -1;
                    }
                }
                long pid = 0;
                long.TryParse(drdProcess.SelectedValue, out pid);
                details[i].Id = id;
                details[i].Price = txtPrice.Text;
                details[i].Quantity = txtQuantity.Text;
                details[i].ProcessId = pid;
            }
            catch
            {
            }
        }
    }
}